//
//  NSString+Common.m
//  Tools
//
//  Created by han on 2019/2/14.
//  Copyright © 2019 han. All rights reserved.
//

#import "NSString+Common.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Common)
- (BOOL)isEmpty {
    if (nil == self
        || ![self isKindOfClass:[NSString class]]
        || [self stringByTrimming].length <= 0) {
        return NO;
    }
    return YES;
}
//trimming
- (NSString *)stringByTrimming {
    NSString *string = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
    return string;
}

- (NSString *)stringByTrimmingExtraSpaces {
    NSArray *array = [self componentsSeparatedByString:@" "];
    NSMutableArray *marray = [NSMutableArray new];
    for (NSString *stringOne in array) {
        //删除字符串中的所有空格
        NSString *zufuchuan = [[stringOne stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        if (zufuchuan.length != 0) {
            [marray addObject:stringOne];
        }
    }
    
    NSString *mString = [marray componentsJoinedByString:@" "];
    //去除 字符串两端的空格 和回车
    mString = [mString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
    return mString;
}
//加密
- (NSString *)stringByMd5 {
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

- (NSString*)stringBySha1 {
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

//计算size
- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    CGSize resultSize = CGSizeZero;
    if (self.length <= 0) {
        return  resultSize;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    resultSize = [self boundingRectWithSize:CGSizeMake(floor(size.width), floor(size.height)) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
    resultSize = CGSizeMake(floor(resultSize.width + 1), floor(resultSize.height + 1));
    return resultSize;
}
- (CGFloat)getHeightWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    return [self getSizeWithFont:font constrainedToSize:size].height;
}
- (CGFloat)getWidthWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    return [self getSizeWithFont:font constrainedToSize:size].width;
}

// attribute
- (NSAttributedString *)attributeStringWithColor:(UIColor *)color {
    if (![self isEmpty]) {
        return nil;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:2];
    //    paragraphStyle.lineBreakMode =  NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;// 此处设置后，高度计算不准确
    NSMutableDictionary *attribute = @{}.mutableCopy;
    attribute[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    attribute[NSForegroundColorAttributeName] = color;
    attribute[NSParagraphStyleAttributeName] = paragraphStyle;
    return [[NSAttributedString alloc] initWithString:self attributes:attribute];
}
- (NSAttributedString *)attributeStringWithColor:(UIColor *)color keywords:(NSArray *)keywords keywordsColor:(UIColor *)keywordsColor {
    if (![self isEmpty]) {
        return nil;
    }
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:2];
    //    paragraphStyle.lineBreakMode =  NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;
    [attr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self length])];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, self.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, self.length)];
    for (NSString *key in keywords) {
        [attr addAttribute:NSForegroundColorAttributeName value:keywordsColor range:[self rangeOfString:key]];
    }
    return attr;
}

//是否电话号码
- (BOOL)isPhoneNO{
    //    NSString *phoneRegex = @"1[3|5|7|8|][0-9]{9}";
    NSString *phoneRegex = @"[0-9]{1,15}";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

@end
